package net.zjitc.controller.portal;

import net.zjitc.common.Const;
import net.zjitc.common.ServiceResponse;
import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author 赵静静
 * @date 2019/3/22 12:00
 */

@Controller
@RequestMapping("/user/")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 登录功能
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<User> login(String username, String password, HttpSession session){
        ServiceResponse<User> response = userService.login(username, password);
        if (response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServiceResponse.createBySuccessMessage("退出成功");
    }

    /**
     * 校验数据的有效性，
     * @param str
     * @param type 校验的数据类型
     * @return
     */
    @RequestMapping(value = "checkVaild.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> register(String  str,String type){
        ServiceResponse<String> response = userService.checkValid(str,type);
        return response;
    }

    /**
     * 注册用户信息
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> register(User user){
        ServiceResponse<String> response = userService.register(user);
        return response;
    }

    /**
     * 当前用户信息
     * @param session
     * @return
     */
    @RequestMapping(value = "getUserInfo.do",method = RequestMethod.GET)
    @ResponseBody
    public ServiceResponse<User> getUserInfo(HttpSession session){
       User user = (User) session.getAttribute(Const.CURRENT_USER);
       if (user!= null){
           return ServiceResponse.createBySuccess(user);
       }
        return ServiceResponse.createByErrorMessage("用户末登录，无法获取用户信息，请先登录");
    }

    /**
     *  输入用户名，获得问题
     * @param username
     * @return
     */
    @RequestMapping(value = "forgetGetQuestion.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> forgetGetQuestion(String username){
        ServiceResponse<String> response = userService.selectQuestion(username);
        return response;
    }

    /**
     * 根据用户名、问题、答案验证是否可以修改密码
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value = "checkAnswer.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> checkAnswer(String username,String question,String answer){
        ServiceResponse<String> response = userService.checkAnswer(username, question, answer);
        return response;
    }

    /**
     * 忘记密码时的修改密码
     * @param username
     * @param newPassword
     * @param forgetToken
     * @return
     */
    @RequestMapping(value = "forgetResetPassword.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> forgetResetPassword(String username,String newPassword,String forgetToken){
        ServiceResponse<String> response = userService.forgetRestPassword(username, newPassword, forgetToken);
        return response;
    }

    /**
     * 登录状态下的修改密码
     * @param oldPassword
     * @param newPassword
     * @param session
     * @return
     */
    @RequestMapping(value = "resetPassword.do",method = RequestMethod.POST)
    @ResponseBody
    public ServiceResponse<String> resetPassword(String oldPassword,String newPassword,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user==null){
            return ServiceResponse.createByErrorMessage("用户未登录");
        }
        ServiceResponse<String> response = userService.resetPassword(oldPassword,newPassword,user);
        return response;
    }
}
